# env /bin/python
# -*- coding:utf-8 -*-
from Tkinter import *
import tkMessageBox
import pyperclip
import sys
import threading
import platform

reload(sys)
sys.setdefaultencoding('utf-8')

window = Tk()
window.geometry('%sx%s+%s+%s' % (440,290, \
                                 (window.winfo_screenwidth() - window.winfo_width())/2, \
                                 (window.winfo_screenheight() - window.winfo_height())/2))
window.title("删除换行脚本")

close = False

# 结束符号
ends = ['.', ':', '!', ';', '?']

# 使能
enable = True
# 处理好的字符串，用于比较
outstr = ''
enteropr='\n'
if platform.system() == "Windows":
	enteropr='\r\n'

# 去除回车符
def do(text):
	paper = text.replace('-' + enteropr, '')
	paper = paper.replace(enteropr, ' ')
	return paper


# 粘贴
def copy():
	global copy_txt
	pyperclip.copy(copy_txt.get('1.0', 'end'))


# 使能
def flag():
	global enable, enable_btn
	enable = not enable
	if enable:
		enable_btn['text'] = '已开始\n监视\n剪贴板'
		pyperclip.copy('')
	else:
		enable_btn['text'] = '已关闭\n监视\n剪贴板'
	threading.Timer(0.5, monitor).start()


# 判断段落是否到结尾
def checkover(paper):
	paper = paper.rstrip(' ')
	if len(paper) == 0: return False
	if paper[-1] in ends:
		return True
	else:
		return False


def about():
	tkMessageBox.showinfo('关于', '可以通过两种方式使用：\n1、直接正常复制；\n2、使用程序提供的文本框；\n\t\t信南204')


def clear():
	global copy_txt
	copy_txt.delete('1.0', 'end')


# 监视剪贴板
def monitor():
	global close, enable, copy_txt, outstr, multcopy
	data = pyperclip.paste()
	if enable and not close:
		if not outstr == data and not copy_txt.get('1.0', 'end') == data:
			outstr = do(data)
			if checkover(outstr):
				copy_txt.insert('end', outstr + '\n')
			else:
				copy_txt.insert('end', outstr)
			pyperclip.copy(outstr)
		threading.Timer(0.5, monitor).start()


enable_btn = Checkbutton(window, text='已开始\n监视\n剪贴板', command=flag)
enable_btn.select()

copy_btn = Button(window, text='复制所有', command=copy)
copy_txt = Text(window, width=50, height=20)
about_btn = Button(window, text='关于', command=about)
clear_btn = Button(window, text='清空', command=clear)

# 布局空间
# 使能多选框
enable_btn.grid(row=3, column=2)

# 复制按钮
copy_btn.grid(row=1, column=2)

about_btn.grid(row=4, column=2)
clear_btn.grid(row=2, column=2)

# 复制的文本区域
copy_txt.grid(row=1, column=1, rowspan=4)
# copy_txt.config(state='disabled')

# 监视剪贴板的线程
threading.Timer(0.5, monitor).start()

{
	# 初始化
	pyperclip.copy('')
}
window.mainloop()

# 未设置临界区可能会出问题
close = True
